package dongze.com.ckdj.ui;

import android.content.Context;
import android.support.v7.widget.AppCompatAutoCompleteTextView;
import android.text.InputFilter;
import android.text.Spanned;
import android.util.AttributeSet;
import android.widget.AutoCompleteTextView;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by fanqwz on 2017/9/28.
 */

public class NoSpecialCharactorAutoCompleteTextView extends AppCompatAutoCompleteTextView {
    String speChat = "[^a-zA-Z0-9]";
    Pattern pattern = Pattern.compile(speChat);

    public NoSpecialCharactorAutoCompleteTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        setFilters(new InputFilter[]{new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                Matcher matcher = pattern.matcher(source.toString());
                if (matcher.find()) return matcher.replaceAll("");
                else return source;
            }
        },
        new InputFilter.LengthFilter(17)});
    }

}
